import {defineStore} from 'pinia'
//引入接口
import { reqGetCode,reqLogin } from '@/api/User';
//引入接口类型
import type { responseCode,reqLoginData,responseLoginDataAll } from '@/api/User/type';
//引入方法
import { GET_TOKEN,GET_USER_NAME,SET_TOKEN,CLEAR_TOKEN } from '@/utils/getToken';
const useUserStore = defineStore('UserStore',{
    state:()=>{
        return {
            //对话框组件的显示与隐藏
            dialogFormVisible:false,
            userCode:'',
            //用户名称
            UserName:GET_USER_NAME(),
            //token
            token:GET_TOKEN(),
        }
    },
    actions:{
        //获取验证码
        async getCode(code:string) {
            let reslut:responseCode = await reqGetCode(code)
           if(reslut.code === 200) {
            this.userCode = reslut.data
            return 'ok'
           }else {
            return Promise.reject(new Error(reslut.message))
           }
            
        },
        //用户登录
        async Login(data:reqLoginData){
            let reslut:responseLoginDataAll = await reqLogin(data)
           if(reslut.code==200) {
            this.UserName = reslut.data.name;
            this.token = reslut.data.token;
            // 本地存储用户名称与token
            SET_TOKEN('UserName',this.UserName)
            SET_TOKEN('token',this.token)
            return 'ok'
           }else {
            return Promise.reject(new Error(reslut.message))
           }
            
        },
        //用户退出登录
        LogoOut() {
            this.UserName = '';
            this.token = '';
            //清除本地存储
            CLEAR_TOKEN('UserName')
            CLEAR_TOKEN('token')
        }
    },
    getters:{},
})


export default useUserStore